package com.agent.commons.enums;

import com.agent.commons.exception.BaseException;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

/**
 *  身份枚举类
 */
@Getter
@AllArgsConstructor
public enum Role {
    Student("student"),
    Teacher("teacher"),
    Admin("admin");

    private final String name;

    /**
     * 根据name获取枚举类型
     */
    private static Role forName(String name){
        for(Role identity : Role.values()){
            if(identity.getName().equals(name)){
                return identity;
            }
        }
        return null;
    }

    /**
     * 判断身份在不在范围内
     * @param role
     * @return
     */
    private static boolean contains(Role role){
        for(Role identity : Role.values()){
            if(identity.equals(role)){
                return true;
            }
        }
        return false;
    }

    /**
     * 获取枚举身份对应名称
     * @param role
     * @return
     */
    private static String getName(Role role){
        if(!contains(role)){
            throw new BaseException(501,"身份不存在");
        }
        return role.getName();
    }
}
